package chen.videoedit;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;
import android.view.View;

import java.util.ArrayList;

import chen.videoedit.continuousplay.DecoderVideoThread;
import chen.videoedit.play.MyGLSurfaceView;

public class ContinuousPlayVideoActivity extends BasisActivity implements SurfaceTexture.OnFrameAvailableListener {


    private MyGLSurfaceView surfaceView;
    private SurfaceTexture surfaceTexture;
    private Surface surface;
    private DecoderVideoThread decoder;
    private ArrayList<String> list;
    private boolean playingFlag;

    @Override
    public int setlayoutResID() {
        return R.layout.activity_continuous_play_video;
    }

    @Override
    public void initialView(Bundle arg0) {
        findViewById(R.id.activity_continuous_play_video_bt).setOnClickListener(this);
        surfaceView = (MyGLSurfaceView) findViewById(R.id.activity_continuous_play_video_glsurfaceview);

    }

    @Override
    public void setActivity() {
        String video = Environment.getExternalStorageDirectory().getPath();//视频
        list = new ArrayList<>();
        list.add(video + "/1.mp4");
        list.add(video + "/2.mp4");
        list.add(video + "/3.mp4");
        list.add(video + "/4.mp4");
    }

    @Override
    public void onClick(View v) {
        startVideo();
    }


    //prepare before playing every video.
    public void prepareVideo() {

        surfaceTexture = surfaceView.getSurfaceTexture();//get surfacetexture created in renderer
        surfaceTexture.setOnFrameAvailableListener(this);
        surface = new Surface(surfaceTexture);//get the surface to be used for decoding output

        decoder = new DecoderVideoThread(list, surface);//initialize all the decoding stuff here.
    }

    public void startVideo() {
        if (decoder != null) stopVideo();
        prepareVideo();
        decoder.startPlaying();//
        Log.e("lll","播放");
        playingFlag = true;
    }

    public void stopVideo() {
        if (decoder != null) {
            decoder.stopPlaying();//delete the thread
            decoder = null;
            playingFlag = false;
        }
    }

    public boolean isPlaying() {
        return playingFlag;
    }

    @Override
    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
        surfaceView.requestRender();
    }
}
